/*
 * @lc app=leetcode.cn id=203 lang=typescript
 *
 * [203] 移除链表元素
 */

// @lc code=start
/**
 * Definition for singly-linked list.
 * class ListNode {
 *     val: number
 *     next: ListNode | null
 *     constructor(val?: number, next?: ListNode | null) {
 *         this.val = (val===undefined ? 0 : val)
 *         this.next = (next===undefined ? null : next)
 *     }
 * }
 */

function removeElements(head: ListNode | null, val: number): ListNode | null {
    if (head === null) return null;
    let newH = new ListNode();
    let keep = newH;
    let now: ListNode | null = head;
    while (now) {
        if (now.val !== val) {
            keep.next = new ListNode(now.val);
            keep = keep.next;
        }
        now = now.next;
    }
    return newH.next;
}
// @lc code=end
